package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.maimengmami.waveswiperefreshlayout.WaveSwipeRefreshLayout;
import com.shiguo365.market.R;
import com.shiguo365.market.bean.Product;
import com.shiguo365.market.display.adapter.AddressListAdapter;
import com.shiguo365.market.display.adapter.ProductListAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IProductListView;
import com.shiguo365.market.presenter.contract.IProductListPresenter;
import com.shiguo365.market.presenter.implement.ProductListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductListActivity extends BaseActivity  implements IProductListView,WaveSwipeRefreshLayout.OnRefreshListener,ProductListAdapter.OnItemClickLitener {
    private final String TAG="ProductListActivity";
    private List<Product> productList1=new ArrayList<Product>();
    private String tagCode,action,searchname;
    private IProductListPresenter iProductListPresenter=null;
    private LinearLayoutManager mLayoutManager;
    private int page=0;
    private int size=10;

    @BindView(R.id.product_list_toolbar)
    protected Toolbar toolbar;

    @BindView(R.id.my_recycler_view_productlist)
    protected RecyclerView mRecyclerView;

    @BindView(R.id.product_list_wave_layout)
    protected WaveSwipeRefreshLayout waveSwipeRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this); //ButterKnife初始化
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        Intent intent =getIntent();
        String tagName=intent.getStringExtra("tagName");
        tagCode=intent.getStringExtra("tagCode");
        action=intent.getStringExtra("action");
        searchname=intent.getStringExtra("searchName");

        if(action != null){
            toolbar.setTitle(searchname);
        }else{
            toolbar.setTitle(tagName);
        }

        iProductListPresenter=new ProductListPresenter(this,this);
        ProductListAdapter mAdapter = new ProductListAdapter(productList1);
        mAdapter.setOnItemClickLitener(this);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        mRecyclerView.setAdapter(mAdapter);
        waveSwipeRefreshLayout.setOnRefreshListener(this);
        RefreshUtils.initOnCreate(waveSwipeRefreshLayout, this);
        RefreshUtils.refreshOnCreate(waveSwipeRefreshLayout, this);

    }



    @Override
    public void onRefresh() {
        page=0;
        productList1.clear();
        if(action!=null){
            iProductListPresenter.searchProlistByName(searchname,page,size);
        }else {
            iProductListPresenter.getProlistByTag(tagCode,page,size);
        }

        page++;
    }

    @Override
    public void onLoad() {
        if(action!=null){
            iProductListPresenter.searchProlistByName(searchname,page,size);
        }else {
            iProductListPresenter.getProlistByTag(tagCode,page,size);
        }
        page++;
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canRefresh() {
        return true;
    }


    //获得数据后
    @Override
    public void onGetProlistByTagOk(@NonNull List<JSONObject> result) {
        Log.d(TAG,result.toString());
        for (int i = 0; i < result.size(); i++) {
            JSONObject jo = (JSONObject) result.get(i);
            Product product = new Product();
            product.setId(jo.getInteger("id"));
            product.setName(jo.getString("name"));
            product.setContext(jo.getString("intro"));
            product.setPrice(jo.getDouble("price"));
            product.setSpecname(jo.getString("quantity"));
            product.setImgurl(jo.getString("main_pic"));
            productList1.add(product);
        }
    }

    @Override
    public void onGetProlistByTagFinish() {
        waveSwipeRefreshLayout.setRefreshing(false);
        waveSwipeRefreshLayout.setLoading(false);
        Log.d("ProductListPresenter","累计:"+String.valueOf(productList1.size()));
    }


    @Override
    public void onItemClick(View view, int position) {
        Product product = productList1.get(position);
        Intent intent =new Intent(this, DetailActivity.class);
        String productId = String.valueOf(product.getId());
        intent.putExtra("productId",productId);
        startActivity(intent);
    }

    @Override
    public void onItemLongClick(View view, int position) {

    }
}
